package com.wfy.common.config;

import com.aliyun.mns.client.CloudAccount;
import com.aliyun.mns.client.CloudTopic;
import com.aliyun.mns.client.MNSClient;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * Created with IntelliJ IDEA.
 * User: wangxindong
 * Date: 2017/10/31
 * Time: 14:21
 */
@Configuration
public class AlimnsProducerConfig implements EnvironmentAware {

    // ================================================================
    // Constants
    // ================================================================

    // ================================================================
    // Fields
    // ================================================================

    private String alimnsAccessId;
    private String alimnsAccessKey;
    private String alimnsAccountEndpoint;
    private String alimnsTopicSensorControl;


    // ================================================================
    // Constructors
    // ================================================================

    // ================================================================
    // Methods from/for super Interfaces or SuperClass
    // ================================================================

    @Override
    public void setEnvironment(Environment environment) {
        alimnsAccessId = environment.getProperty("ALIMNS_ACCESS_ID");
        alimnsAccessKey = environment.getProperty("ALIMNS_ACCESS_KEY");
        alimnsAccountEndpoint = environment.getProperty("ALIMNS_ACCOUNT_ENDPOINT");
        alimnsTopicSensorControl = environment.getProperty("ALIMNS_TOPIC_SENSOR_CONTROL");
    }

    // ================================================================
    // Public or Protected Methods
    // ================================================================

    @Bean(name = "cloudAccount")
    public CloudAccount cloudAccount() {
        CloudAccount cloudAccount = new
                CloudAccount(alimnsAccessId, alimnsAccessKey, alimnsAccountEndpoint);
        return cloudAccount;
    }

    @Bean(name = "mnsClient")
    public MNSClient mnsClient(CloudAccount cloudAccount) {
        return cloudAccount.getMNSClient();
    }

    @Bean(name = "cloudTopic")
    public CloudTopic cloudTopic(MNSClient mnsClient) {
        return mnsClient.getTopicRef(alimnsTopicSensorControl);
    }


    // ================================================================
    // Getter & Setter
    // ================================================================

    public String getAlimnsAccessId() {
        return alimnsAccessId;
    }

    public void setAlimnsAccessId(String alimnsAccessId) {
        this.alimnsAccessId = alimnsAccessId;
    }

    public String getAlimnsAccessKey() {
        return alimnsAccessKey;
    }

    public void setAlimnsAccessKey(String alimnsAccessKey) {
        this.alimnsAccessKey = alimnsAccessKey;
    }

    public String getAlimnsAccountEndpoint() {
        return alimnsAccountEndpoint;
    }

    public void setAlimnsAccountEndpoint(String alimnsAccountEndpoint) {
        this.alimnsAccountEndpoint = alimnsAccountEndpoint;
    }

    public String getAlimnsTopicSensorControl() {
        return alimnsTopicSensorControl;
    }

    public void setAlimnsTopicSensorControl(String alimnsTopicSensorControl) {
        this.alimnsTopicSensorControl = alimnsTopicSensorControl;
    }

    // ================================================================
    // Private Methods
    // ================================================================


    // ================================================================
    // Inner or Anonymous Class
    // ================================================================

    // ================================================================
    // Test Methods
    // ================================================================

}
